﻿using System;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.Caching;
using System.Xml;
using log4net;

namespace Hutech.Authentication.Library.Business.WebHelpers.UserRegisteredHandlers
{
    /// <summary>
    ///
    /// </summary>
    public class UserRegisteredHandlerProviderConfig
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(UserRegisteredHandlerProviderConfig));
        private ProviderSettingsCollection providerSettingsCollection
        = new ProviderSettingsCollection();

        public ProviderSettingsCollection Providers
        {
            get
            {
                return providerSettingsCollection;
            }
        }

        public static UserRegisteredHandlerProviderConfig GetConfig()
        {
            try
            {
                if (
                (HttpRuntime.Cache["UserRegisteredHandlerProviderConfig"] != null)
                && (HttpRuntime.Cache["UserRegisteredHandlerProviderConfig"] is UserRegisteredHandlerProviderConfig)
                )
                {
                    return (UserRegisteredHandlerProviderConfig)HttpRuntime.Cache["UserRegisteredHandlerProviderConfig"];
                }

                UserRegisteredHandlerProviderConfig config
                = new UserRegisteredHandlerProviderConfig();

                String configFolderName = "~/Setup/ProviderConfig/userregisteredhandlers/";

                string pathToConfigFolder
                = HttpContext.Current.Server.MapPath(configFolderName);


                if (!Directory.Exists(pathToConfigFolder))
                    return config;

                DirectoryInfo directoryInfo
                = new DirectoryInfo(pathToConfigFolder);

                FileInfo[] configFiles = directoryInfo.GetFiles("*.config");

                foreach (FileInfo fileInfo in configFiles)
                {
                    XmlDocument configXml = new XmlDocument();
                    configXml.Load(fileInfo.FullName);
                    config.LoadValuesFromConfigurationXml(configXml.DocumentElement);
                }

                AggregateCacheDependency aggregateCacheDependency
                = new AggregateCacheDependency();

                string pathToWebConfig
                = HttpContext.Current.Server.MapPath("~/Web.config");

                aggregateCacheDependency.Add(new CacheDependency(pathToWebConfig));

                System.Web.HttpRuntime.Cache.Insert(
                "UserRegisteredHandlerProviderConfig",
                config,
                aggregateCacheDependency,
                DateTime.Now.AddYears(1),
                TimeSpan.Zero,
                System.Web.Caching.CacheItemPriority.Default,
                null);

                return (UserRegisteredHandlerProviderConfig)HttpRuntime.Cache["UserRegisteredHandlerProviderConfig"];
            }
            catch (HttpException ex)
            {
                log.Error(ex);
            }
            catch (System.Xml.XmlException ex)
            {
                log.Error(ex);
            }
            catch (ArgumentException ex)
            {
                log.Error(ex);
            }
            catch (NullReferenceException ex)
            {
                log.Error(ex);
            }

            return null;
        }

        public void LoadValuesFromConfigurationXml(XmlNode node)
        {
            foreach (XmlNode child in node.ChildNodes)
            {
                if (child.Name == "providers")
                {
                    foreach (XmlNode providerNode in child.ChildNodes)
                    {
                        if (
                        (providerNode.NodeType == XmlNodeType.Element)
                        && (providerNode.Name == "add")
                        )
                        {
                            if (
                            (providerNode.Attributes["name"] != null)
                            && (providerNode.Attributes["type"] != null)
                            )
                            {
                                ProviderSettings providerSettings
                                = new ProviderSettings(
                                providerNode.Attributes["name"].Value,
                                providerNode.Attributes["type"].Value);

                                providerSettingsCollection.Add(providerSettings);
                            }
                        }
                    }
                }
            }
        }
    }
}
